package com.apache.funds.common;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.api.vo.ResultEntity;
import com.apache.funds.vo.UserFundsVo;
import com.apache.rpc.util.LoadRpcService;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;

/**
 * 中百信资金账务工具类<br/>
 * ResultEntity统一对象返回结果说明:<br/>
 *  result=代表通信,<br/>
 *  message=描述,<br/>
 *  entity=是对象返回对象,否则返回处理结果（"true"或者"false"）
 * @author xhe
 */
public class FundsUtil {
	private static Logger logger = LoggerFactory.getLogger(FundsUtil.class);

	/**
	 * 资金账务业务处理统一调用【通用的（出入金，冻结，解冻，交割）】（通过PRC方式）<br/>
	 * beanId： fundsService<br/>
	 * methodName： fundsPublicOperate
	 * @param logMsg 日志提示
	 * @param prarmMap 请求参数
	 * @return  ResultEntity
	 */
	public static ResultEntity doFunds(String logMsg, Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
		logger.info("[资金账务统一调用]," + logMsg + "-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsPublicOperate", "XML", fundsMap,
				null);

		//		System.out.println("======>"+JSONObject.fromObject(entity));

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用]," + logMsg + "-end:" + returnObjMsg);
		logger.info("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
		return entity;
	}

	/**
	 * 查询我的余额（从插件，再从缓存）
	 * @param userEname
	 * @return ResultEntity的entity值类型是map对象
	 */
	public static ResultEntity selectFundsMyMoneyByRPC(String userEname) {
		//1.打印日志
		logger.info("[资金账务统一调用],余额查询-start:" + userEname);

		//2.RPC发送请求
		Map<String, Object> fundsMap = new HashMap<String, Object>();
		fundsMap.put("userEname", userEname);
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsMyCache", "XML", fundsMap, null);

		//3.结果处理
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],余额查询-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 查询我的余额(从缓存)
	 * @param userEname
	 * @return
	 */
	public static UserFundsVo selectFundsMyMoneyByCache(String userEname) {
		logger.info("[资金账务统一调用],余额查询-start:" + userEname);

		UserFundsVo userFundsVo = FundsCache.getInstance().getBalanCache(userEname);

		logger.info("[资金账务统一调用],余额查询-end:" + userFundsVo);
		return userFundsVo;
	}

	/**
	 * 流水明细查询：参数map的key说明:<br>
	 * pageIndex,pageSize（分页必传）<br>
	 * transdate,userid,direction,actiontype,actionid,actionname
	 */
	public static ResultEntity selectFundsFlowing(Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("[资金账务统一调用],流水明细查询-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsFlowing", "XML", fundsMap, null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],流水明细查询-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 账户列表查询：参数map的key说明:<br>
	 * pageIndex,pageSize（分页必传）<br>
	 * transdate,userid,direction,actiontype,actionid,actionname
	 */
	public static ResultEntity selectFundsUserList(Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("[资金账务统一调用],账户列表查询-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service()
				.doService("fundsService", "fundsUserList", "XML", fundsMap, null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],账户列表查询-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 操作虚拟账户信息：参数map的key说明:<br>
	 * userEname,userCname<br>
	 * certCode,certType,mobile,contactuser,contactAddress,password
	 */
	public static ResultEntity doFundsOpenAccount(Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("[资金账务统一调用],操作虚拟账户信息-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsOpenAccount", "XML", fundsMap,
				null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],操作虚拟账户信息-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 刷新用户余额缓存（重新读库统计）
	 * @param userEname
	 * @return
	 */
	public static void doFundsCache(String userEname) {
		logger.info("[资金账务统一调用],刷新用户余额缓存（重新读库统计）-start:" + userEname);

		FundsCache.getInstance().initBalanCache(userEname);

		logger.info("[资金账务统一调用],刷新用户余额缓存（重新读库统计）-end");
	}

	/**
	 * 定时器要调用的，资金日终结果设置（期初+发生）
	 * @param dateStr 指定日期
	 * @return
	 */
	public static ResultEntity doFundsTimeTaskBalance(String dateStr) {
		Map<String, Object> fundsMap = new HashMap<String, Object>();
		if (StrUtil.isNotNull(dateStr)) {
			try {
				long dateTrade = DateUtils.parse(dateStr, "yyyyMMdd").getTime();
				fundsMap.put("dateTrade", String.valueOf(dateTrade));
			} catch (ParseException e) {
				e.printStackTrace();
				ResultEntity entity = new ResultEntity();
				entity.setEntity("true");
				entity.setResult("false");
				entity.setMessage("参数dateStr的格式不对，应该是yyyyMMdd");
				logger.info("参数dateStr的格式不对，应该是yyyyMMdd");
				return entity;
			}
		}

		//1.打印日志
		logger.info("[资金账务统一调用],资金日终结果设置（期初+发生）-start：" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsOpenAndOccurBalance", "XML",
				fundsMap, null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],资金日终结果设置（期初+发生）-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 查询我的虚拟账户信息
	 * @param userEname
	 * @return entity.setEntity(是json对象)
	 */
	public static ResultEntity selectFundsMyAccount(String userEname) {
		//1.打印日志
		logger.info("[资金账务统一调用],查询我的虚拟账户信息-start:" + userEname);

		//2.RPC发送请求
		Map<String, Object> fundsMap = new HashMap<String, Object>();
		fundsMap.put("userEname", userEname);
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsMyAccount", "XML", fundsMap,
				null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		logger.info("[资金账务统一调用],查询我的虚拟账户信息-end:" + entity);
		return entity;
	}

	/**
	 * 登录：资金账户或口令校验：参数map的key说明:<br>
	 * userEname,password
	 */
	public static ResultEntity doFundsAccountLogin(Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("[资金账务统一调用],资金账户或口令校验-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsAccountLogin", "XML", fundsMap,
				null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}

		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],资金账户或口令校验-end:" + returnObjMsg);
		return entity;
	}

	/**
	 * 资金账户口令更换：参数map的key说明:<br>
	 * userEname,passold,passnew
	 */
	public static ResultEntity doFundsAccountPass(Map<String, Object> fundsMap) {
		//1.打印日志
		logger.info("[资金账务统一调用],资金账户口令更换-start:" + fundsMap);

		//2.RPC发送请求
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsAccountPass", "XML", fundsMap,
				null);

		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		Object returnObjMsg = JSONObject.fromObject(entity);
		logger.info("[资金账务统一调用],资金账户口令更换-end:" + returnObjMsg);
		return entity;
	}
	
	/**
	 * 根据createUserEname登录账号，查询我的虚拟账户信息
	 * @param createUserEname
	 * @return entity.setEntity(是json对象)
	 */
	public static ResultEntity selectFundsMyAccountByCreateUserEname(String createUserEname) {
		//1.打印日志
		logger.info("[资金账务统一调用],根据createUserEname查询我的虚拟账户信息-start:" + createUserEname);

		//2.RPC发送请求
		Map<String, Object> fundsMap = new HashMap<String, Object>();
		fundsMap.put("createUserEname", createUserEname);
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsMyAccountByCreateUserEname", "XML", fundsMap,
				null);
		
		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		logger.info("[资金账务统一调用],查询我的虚拟账户信息-end:" + entity);
		return entity;
	}
	
	/**
	 * 根据userId登录UCT_USER表的主键，查询我的虚拟账户信息
	 * @param userId
	 * @return entity.setEntity(是json对象)
	 */
	public static ResultEntity selectFundsMyAccountByUserId(String userId) {
		//1.打印日志
		logger.info("[资金账务统一调用],根据userId查询我的虚拟账户信息-start:" + userId);

		//2.RPC发送请求
		Map<String, Object> fundsMap = new HashMap<String, Object>();
		fundsMap.put("userId", userId);
		ResultEntity entity = LoadRpcService.service().doService("fundsService", "fundsMyAccountByUserId", "XML", fundsMap,
				null);
		
		//3.结果处理
		if (entity == null) {
			entity = new ResultEntity();
			entity.setResult("false");
			entity.setMessage("请求资金账务失败！");
			logger.info("请求资金账务失败！");
		}
		logger.info("[资金账务统一调用],查询我的虚拟账户信息-end:" + entity);
		return entity;
	}
}
